<?php

/*
=====================================================
 FluxBB - Delux Board Converstations (DBC)
-----------------------------------------------------
 http://dbc-forum.com/
 http://fluxbb.org/
-----------------------------------------------------
 Copyright (c) 2010,2011 FluxBB - Delux Board Conversations (DBC)
-----------------------------------------------------
 Предсловие: Данный скрипт (DBC) не является работой с нуля, то 
 есть ядром скрипта всё также остаётся FuxBB,но теперь в оболочке
 DBC.
=====================================================
*/

define('DBC_ROOT', dirname(__FILE__).'/');
require DBC_ROOT.'engine/common.php';

if ($DBC_user['g_read_board'] == '0')
	message($lang_common['No view']);


$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id < 1)
	message($lang_common['Bad request']);

// Fetch some info about the post, the topic and the forum
$result = $db->query('SELECT f.id AS fid, f.forum_name, f.moderators, f.redirect_url, fp.post_replies, fp.post_topics, t.id AS tid, t.stick_fp, t.subject, t.topic_desc, t.posted, t.first_post_id, t.closed, p.poster, p.poster_id, p.message, p.hide_smilies, p.posted as pposted, p.edit_post, w.message AS warning FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'warnings AS w ON p.id=w.id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id='.$id) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
	message($lang_common['Bad request']);

$cur_post = $db->fetch_assoc($result);

// Sort out who the moderators are and if we are currently a moderator (or an admin)
$mods_array = ($cur_post['moderators'] != '') ? unserialize($cur_post['moderators']) : array();
$is_admmod = ($DBC_user['g_id'] == DBC_ADMIN || ($DBC_user['g_moderator'] == '1' && array_key_exists($DBC_user['username'], $mods_array))) ? true : false;

$can_edit_subject = $id == $cur_post['first_post_id'];

if ($DBC_config['o_censoring'] == '1')
{
	$cur_post['subject'] = censor_words($cur_post['subject']);
	$cur_post['topic_desc'] = censor_words($cur_post['topic_desc']);
	$cur_post['message'] = censor_words($cur_post['message']);
}

// Do we have permission to edit this post?
if (($DBC_user['g_edit_posts'] == '0' ||
	$cur_post['poster_id'] != $DBC_user['id'] ||
	$cur_post['closed'] == '1') &&
	!$is_admmod)
	message($lang_common['No permission']);

// мод ограничения времени редактирвания
if (!$is_admmod && $DBC_user['g_deledit_interval'] != 0 && $cur_post['edit_post'] != 1 && (time()-$cur_post['pposted']) > $DBC_user['g_deledit_interval'])
	message($lang_common['No permission']);

// Load the post.php/edit.php language file
require DBC_ROOT.'language/'.$DBC_user['language'].'/post.php';

// Start with a clean slate
$errors = array();

if (isset($_POST['form_sent']))
{
	if ($is_admmod)
		confirm_referrer('edit.php');

	// If it's a topic it must contain a subject
		$topic_desc = DBC_trim($_POST['topic_desc']);

	if ($can_edit_subject)
	{
		$subject = DBC_trim($_POST['req_subject']);

		if ($subject == '')
			$errors[] = $lang_post['No subject'];
		else if (DBC_strlen($subject) > 70)
			$errors[] = $lang_post['Too long subject'];
		else if ($DBC_config['p_subject_all_caps'] == '0' && is_all_uppercase($subject) && !$DBC_user['is_admmod'])
			$errors[] = $lang_post['All caps subject'];
		
	} else {
		$subject = $cur_post['subject'];
		$topic_desc = $cur_post['topic_desc'];
	}

	// Clean up message from POST
	$message = DBC_linebreaks(DBC_trim($_POST['req_message']));

	// Here we use strlen() not DBC_strlen() as we want to limit the post to DBC_MAX_POSTSIZE bytes, not characters
	if (DBC_strlen($message) > DBC_MAX_POSTSIZE)
		$errors[] = sprintf($lang_post['Too long message'], forum_number_format(DBC_MAX_POSTSIZE));
	else if ($DBC_config['p_message_all_caps'] == '0' && is_all_uppercase($message) && !$DBC_user['is_admmod'])
		$errors[] = $lang_post['All caps message'];

	// Validate BBCode syntax
	if ($DBC_config['p_message_bbcode'] == '1')
	{
		require DBC_ROOT.'engine/parser.php';
		$message = preparse_bbcode($message, $errors);
	}

	if ($message == '')
		$errors[] = $lang_post['No message'];

	$hide_smilies = isset($_POST['hide_smilies']) ? '1' : '0';
	$edit_post = isset($_POST['editpost']) ? '1' : '0';

	// Did everything go according to plan?
	if (empty($errors) && !isset($_POST['preview']))
	{
		// MOD warnings
		$is_modified = $subject != $cur_post['subject'] || $topic_desc != $cur_post['topic_desc'] || $message != $cur_post['message'] || $hide_smilies != $cur_post['hide_smilies'] || $edit_post != $cur_post['edit_post'];
		$is_modified_s = $subject != $cur_post['subject'] || $topic_desc != $cur_post['topic_desc'] || (isset($_POST['stickfp']) && $cur_post['stick_fp'] != 1) || (!isset($_POST['stickfp']) && $cur_post['stick_fp'] == 1);

		$edited_sql = (!isset($_POST['silent']) || !$is_admmod) ? $edited_sql = ', edited='.time().', edited_by=\''.$db->escape($DBC_user['username']).'\'' : '';
		// мод ограничения времени редактирвания 
		$edited_sql .= ($DBC_user['g_id'] == DBC_ADMIN) ? ', edit_post='.$edit_post : '';

		require DBC_ROOT.'engine/search_idx.php';

	if ($can_edit_subject)
		if ($is_modified_s && $can_edit_subject)
		{
			$stick_fp = '';
			if ($is_admmod && isset($_POST['stickfp']))
				$stick_fp = 'stick_fp=1, ';
			else if ($is_admmod)
				$stick_fp = 'stick_fp=0, ';

			// Update the topic and any redirect topics
			$db->query('UPDATE '.$db->prefix.'topics SET '.$stick_fp.'subject=\''.$db->escape($subject).'\', topic_desc=\''.$db->escape($topic_desc).'\' WHERE id='.$cur_post['tid'].' OR moved_to='.$cur_post['tid']) or error('Unable to update topic', __FILE__, __LINE__, $db->error());

			// We changed the subject, so we need to take that into account when we update the search words
			update_search_index('edit', $id, $message, $subject, $topic_desc);
		}
		else if ($is_modified)
			update_search_index('edit', $id, $message, $topic_desc);

		if ($is_admmod)
		{
			$warning = DBC_linebreaks(DBC_trim($_POST['warning']));
			if ($warning != $cur_post['warning'])
			{
				$db->query('DELETE FROM '.$db->prefix.'warnings WHERE id='.$id) or error('Unable to remove warning', __FILE__, __LINE__, $db->error());
				$sql_warm = '';
				if (strlen($_POST['warning']) > 0 )
				{
					$db->query('INSERT INTO '.$db->prefix.'warnings (id, poster, poster_id, posted, message) VALUES('.$id.', \''.$db->escape($DBC_user['username']).'\', '.$DBC_user['id'].', '.time().', \''.$db->escape($_POST['warning']).'\')') or error('Unable to insert warning', __FILE__, __LINE__, $db->error());
					$sql_warm = ', warning_flag=1';
				}
				$result = $db->query('SELECT COUNT(p.id) FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'warnings AS w ON w.id=p.id WHERE p.poster_id='.$cur_post['poster_id']) or error('Unable to sum for posts', __FILE__, __LINE__, $db->error());
				$num_warn = $db->result($result);
				$db->query('UPDATE '.$db->prefix.'users SET warning_all='.$num_warn.$sql_warm.' WHERE id='.$cur_post['poster_id']) or error('Unable to update user', __FILE__, __LINE__, $db->error());
			}
		}

		if ($is_modified)
		{
			// Update the post
			$db->query('UPDATE '.$db->prefix.'posts SET message=\''.$db->escape($message).'\', hide_smilies='.$hide_smilies.$edited_sql.' WHERE id='.$id) or error('Unable to update post', __FILE__, __LINE__, $db->error());
		}
		redirect('topics_'.$id.'#p'.$id, $lang_post['Edit redirect']);
	}
}


// подключаем скрипт для бб-кодов - Visman
$page_head['bbcode'] = '<script type="text/javascript" src="engine/classes/js/post.js"></script>';

$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']), $lang_post['Edit post']);
$required_fields = array('req_subject' => $lang_common['Subject'], 'req_topic_subject' => $lang_common['Topic_subject'], 'req_message' => $lang_common['Message']);
$focus_element = array('edit', 'req_message');
define('DBC_ACTIVE_PAGE', 'index');
define('DBC_EDIT', 1);
require DBC_ROOT.'engine/engine.php';

$cur_index = 1;

?>
<div id="sec_nav_con"><div id="sec_nav_left"><div id="sec_nav_right"><div id="secondary_nav">
<ul id="breadcrumb" class="left">
<li class="first"><a href="index.php"><?php echo $lang_common['Index'] ?></a>></li>
<li><span>»&#160;</span><a href="forum_<?php echo $cur_post['fid'] ?>"><?php echo DBC_htmlspecialchars($cur_post['forum_name']) ?></a></li>
<li><span>»&#160;</span><a href="topic_<?php echo $cur_post['tid'] ?>"><?php echo DBC_htmlspecialchars($cur_post['subject']) ?></a></li>
<li><span>»&#160;</span><strong><?php echo $lang_post['Edit post'] ?></strong></li>
</ul>
<ul id="section_links" class="right"></ul><br class="clear">
</div></div></div></div><br/>

<?php

// If there are errors, we display them
if (!empty($errors))
{

?>
<div id="posterror" class="block">

<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_post['Post errors'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></table>

<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv_c"><br/>


		<div class="inbox error-info">
			<p><?php echo $lang_post['Post errors info'] ?></p>
			<ul class="error-list">
<?php

	foreach ($errors as $cur_error)
		echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
?>
			</ul>
		</div>
<br/>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>
</div>

<?php

}
else if (isset($_POST['preview']))
{
	require_once DBC_ROOT.'engine/parser.php';
	$preview_message = parse_message($message, $hide_smilies);

?>


	<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_post['Post preview'] ?></span></td>
	<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></table>
	
	<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" width="100%"><div class="forum_adv_c">
	
	<?php echo $preview_message."\n" ?>

	<br/>
	</div></td>
	<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</td></tr></table>
	<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></thead></table>


<?php

}

?>
<div id="editform" class="blockform">

<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_post['Edit post'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></table>

<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv2"><div style="padding: 0px 25px 0px 25px;">

		<form id="edit" method="post" action="edit_<?php echo $id ?>&amp;action=edit" onsubmit="return process_form(this)">
					<input type="hidden" name="form_sent" value="1" />
					<input type="hidden" name="csrf_hash" value="<?php echo csrf_hash() ?>" />
<table class="tableform">

<?php if ($can_edit_subject): ?>
		<tr class="fieldtr">

			<td class="label"><?php echo $lang_common['Subject'] ?></td>
			<td><input class="f_input" type="text" name="req_subject" size="80" maxlength="70" tabindex="<?php echo $cur_index++ ?>" value="<?php echo DBC_htmlspecialchars(isset($_POST['req_subject']) ? $_POST['req_subject'] : $cur_post['subject']) ?>" />
			</td>
		</tr>
<?php endif; ?>

		<tr class="fieldtr">

			<td class="label"><?php echo $lang_common['topic_desc'] ?></td>
			<td><input class="f_input" type="text" name="topic_desc" value="<?php echo DBC_htmlspecialchars(isset($_POST['topic_desc']) ? $_POST['topic_desc'] : $cur_post['topic_desc']) ?>" size="80" maxlength="70" tabindex="<?php echo $cur_index++ ?>" />
			</td>
		</tr>

<script type="text/javascript">
	jQuery(function()
	{
		jQuery('textarea').autoResize();
	});
</script>

		<tr class="fieldtr">

			<td class="label"><?php echo $lang_common['Message'] ?></td>
			<td><div id="bbcode_bar"></div><textarea class="f_editor  message" name="req_message" rows="20" cols="95" style="width: 100%" tabindex="<?php echo $cur_index++ ?>"><?php echo DBC_htmlspecialchars(isset($_POST['req_message']) ? $message : $cur_post['message']) ?></textarea>
						<ul class="bblinks">
							<li><span><a href="help.php#bbcode" onclick="window.open(this.href); return false;"><?php echo $lang_common['BBCode'] ?></a> <?php echo ($DBC_config['p_message_bbcode'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
							<li><span><a href="help.php#img" onclick="window.open(this.href); return false;"><?php echo $lang_common['img tag'] ?></a> <?php echo ($DBC_config['p_message_img_tag'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
							<li><span><a href="help.php#smilies" onclick="window.open(this.href); return false;"><?php echo $lang_common['Smilies'] ?></a> <?php echo ($DBC_config['o_smilies'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
						</ul>
			</td>
		</tr>
<?php if ($is_admmod): ?>	
		<tr class="fieldtr">

			<td class="label"><?php echo $lang_common['Moderator'] ?></td>
			<td><input class="f_input" type="text" name="warning" size="80" maxlength="5000" tabindex="<?php echo $cur_index++ ?>" value="<?php echo DBC_htmlspecialchars(isset($_POST['warning']) ? $_POST['warning'] : $cur_post['warning']) ?>" />
					<?php elseif ($cur_post['warning'] != ''): ?>
						<p><div class="postwarn">
							<?php echo DBC_htmlspecialchars($cur_post['warning'])."\n" ?>
						</div></p>
					<?php endif; ?>
			</td>
		</tr>


<?php

$checkboxes = array();
if ($DBC_config['o_smilies'] == '1')
{
	if (isset($_POST['hide_smilies']) || $cur_post['hide_smilies'] == '1')
		$checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" checked="checked" tabindex="'.($cur_index++).'" />'.$lang_post['Hide smilies'].'<br /></label>';
	else
		$checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['Hide smilies'].'<br /></label>';
}

if ($is_admmod)
{
	if ((isset($_POST['form_sent']) && isset($_POST['silent'])) || !isset($_POST['form_sent']))
		$checkboxes[] = '<label><input type="checkbox" name="silent" value="1" tabindex="'.($cur_index++).'" checked="checked" />'.$lang_post['Silent edit'].'<br /></label>';
	else
		$checkboxes[] = '<label><input type="checkbox" name="silent" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['Silent edit'].'<br /></label>';
	// StickFP
	if ($can_edit_subject)
	{
		if ((isset($_POST['form_sent']) && isset($_POST['stickfp'])) || (!isset($_POST['form_sent']) && $cur_post['stick_fp'] != 0))
			$checkboxes[] = '<label><input type="checkbox" name="stickfp" value="1" tabindex="'.($cur_index++).'" checked="checked" />'.$lang_post['Stick first post'].'<br /></label>';
		else
			$checkboxes[] = '<label><input type="checkbox" name="stickfp" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['Stick first post'].'<br /></label>';
	}
	// StickFP
}

if ($DBC_user['g_id'] == DBC_ADMIN)
{
	if ((isset($_POST['form_sent']) && !isset($_POST['editpost'])) || (!isset($_POST['form_sent']) && $cur_post['edit_post'] != 1))
		$checkboxes[] = '<label><input type="checkbox" name="editpost" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['EditPost edit'].'<br /></label>';
	else
		$checkboxes[] = '<label><input type="checkbox" name="editpost" value="1" tabindex="'.($cur_index++).'" checked="checked" />'.$lang_post['EditPost edit'].'<br /></label>';
}

if (!empty($checkboxes))
{

?>


		<tr class="fieldtr">

			<td class="label"><?php echo $lang_common['Options'] ?></td>
			<td><?php echo implode("\n\t\t\t\t\t\t\t", $checkboxes)."\n" ?>
			</td>
		</tr>




<?php

	}

?>

		<tr class="fieldtr">

			<td class="label"></td>
			<td>			<p class="buttons"><input type="submit" class="fbutton" name="submit" value="<?php echo $lang_common['Submit'] ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="s" /> <input type="submit" class="fbutton" name="preview" value="<?php echo $lang_post['Preview'] ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="p" /> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
			</td>
		</tr>


	</table>
		</form>
</div>
<br/>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>
</div>
<?php
require DBC_ROOT.'engine/bbcode.inc.php';
$footer_style = 'index';
require DBC_ROOT.'engine/footer.php';